package org.jeecg.modules.vedio_order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.medication_order.vo.MedicationOrderVo;
import org.jeecg.modules.vedio_order.entity.VedioOrder;
import org.jeecg.modules.vedio_order.service.IVedioOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

 /**
 * @Description: 视频订单
 * @Author: jeecg-boot
 * @Date:   2025-07-05
 * @Version: V1.0
 */
@Api(tags="视频订单")
@RestController
@RequestMapping("/vedio_order/vedioOrder")
@Slf4j
public class VedioOrderController extends JeecgController<VedioOrder, IVedioOrderService> {
	@Autowired
	private IVedioOrderService vedioOrderService;
	
	/**
	 * 分页列表查询
	 *
	 * @param vedioOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "视频订单-分页列表查询")
	@ApiOperation(value="视频订单-分页列表查询", notes="视频订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<VedioOrder>> queryPageList(VedioOrder vedioOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VedioOrder> queryWrapper = QueryGenerator.initQueryWrapper(vedioOrder, req.getParameterMap());
		Page<VedioOrder> page = new Page<VedioOrder>(pageNo, pageSize);
		IPage<VedioOrder> pageList = vedioOrderService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param vedioOrder
	 * @return
	 */
	@AutoLog(value = "视频订单-添加")
	@ApiOperation(value="视频订单-添加", notes="视频订单-添加")
	@RequiresPermissions("vedio_order:vedio_order:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody VedioOrder vedioOrder) {
		vedioOrderService.save(vedioOrder);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param vedioOrder
	 * @return
	 */
	@AutoLog(value = "视频订单-编辑")
	@ApiOperation(value="视频订单-编辑", notes="视频订单-编辑")
	@RequiresPermissions("vedio_order:vedio_order:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody VedioOrder vedioOrder) {
		vedioOrderService.updateById(vedioOrder);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "视频订单-通过id删除")
	@ApiOperation(value="视频订单-通过id删除", notes="视频订单-通过id删除")
	@RequiresPermissions("vedio_order:vedio_order:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		vedioOrderService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "视频订单-批量删除")
	@ApiOperation(value="视频订单-批量删除", notes="视频订单-批量删除")
	@RequiresPermissions("vedio_order:vedio_order:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.vedioOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "视频订单-通过id查询")
	@ApiOperation(value="视频订单-通过id查询", notes="视频订单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<VedioOrder> queryById(@RequestParam(name="id",required=true) String id) {
		VedioOrder vedioOrder = vedioOrderService.getById(id);
		if(vedioOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(vedioOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param vedioOrder
    */
    @RequiresPermissions("vedio_order:vedio_order:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VedioOrder vedioOrder) {
        return super.exportXls(request, vedioOrder, VedioOrder.class, "视频订单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("vedio_order:vedio_order:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VedioOrder.class);
    }


	 /**
	  * 微信用户订单
	  *
	  * @param vedioOrder
	  * @return
	  */
	 @AutoLog(value = "视频订单-添加")
	 @ApiOperation(value = "视频订单-添加", notes = "视频订单-添加")
	 @PostMapping(value = "/addByWeChat")
	 public Result<?> addByWeChat(@RequestBody VedioOrder vedioOrder,HttpServletRequest req) {
		 String org = req.getHeader("sys_org_code");
		 String mchId = "A01A03".equals(org.substring(0,6))?"1526566731":"1513682771";
		 vedioOrder.setSysOrgCode(org);
		 BaseMap map = vedioOrderService.addByWeChat(vedioOrder,mchId);
		 return Result.OK(map);
	 }

	 /**
	  * 微信支付用户订单
	  *
	  * @param outTradeNo
	  * @return
	  */
	 @AutoLog(value = "微信用户订单-支付")
	 @ApiOperation(value = "微信用户订单-支付", notes = "微信用户订单-支付")
	 @PostMapping(value = "/payByWeChat")
	 public Result<?> payByWeChat(@RequestParam(name = "outTradeNo") String outTradeNo) {
		 BaseMap map = vedioOrderService.payByWeChat(outTradeNo);
		 return Result.OK(map);
	 }

	 /**
	  * 微信列表查询
	  *
	  * @param beginDate
	  * @param endDate
	  * @param userId
	  * @return
	  */
	 //@AutoLog(value = "药房订单-微信列表查询")
	 @ApiOperation(value = "视频订单-微信列表查询", notes = "视频订单-微信列表查询")
	 @GetMapping(value = "/listByWeChat")
	 public Result<?> listByWeChat(
			 @RequestParam(name = "beginDate", required = false) String beginDate,
			 @RequestParam(name = "endDate", required = false) String endDate,
			 @RequestParam(name = "userId", required = true) String userId,
			 @RequestParam(name = "vedioId", required = true) String vedioId,
			 @RequestParam(name = "status", required = true) String status
	 ) {
		 List<Map<String, Object>> list = vedioOrderService.listByWeChat(beginDate, endDate,  userId, vedioId,status);
		 return Result.ok(list);
	 }

	 /**
	  * 微信用户订单支付回调
	  *
	  * @param request
	  * @return
	  */
	 @AutoLog(value = "微信用户订单-支付回调")
	 @ApiOperation(value = "微信用户订单-支付回调", notes = "微信用户订单-支付回调")
	 @PostMapping(value = "/payNotify")
	 public Result<?> payNotify(HttpServletRequest request) {
		 vedioOrderService.payNotify(request,"");
		 return Result.OK();
	 }


 }
